package com.student.controller;

import com.student.pojo.User;
import com.student.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
/**
 * @author 是一个鸽子啊
 * @date 2020/4/14
 * @qq 364826415
 */
@Controller
public class UserController {
    @Autowired
    @Qualifier("userServiceImpl")
    private UserService userService;
    @RequestMapping("/loginUser")
    public String loginUser(HttpServletRequest request,String userName,String userPassword){
        User user=userService.loginUser(userName, userPassword);
        if (user !=null){
            request.getSession().setAttribute("user",user);
            return "/index";
        }else {
            request.setAttribute("msg","账号名或密码错误");
            return "/admin-login";
        }
    }
    @RequestMapping("/queryUserList")
    private  String queryUserList(HttpServletRequest request){
        List<User> userList = userService.queryUserList();
        request.setAttribute("userList",userList);
        System.out.println(userList);
        return "/user-table";

    }
    @RequestMapping("/updateUser")
    private String updateUser(User user,HttpServletRequest request){
        int i = userService.updateUser(user);
        List<User> userList = userService.queryUserList();
        request.setAttribute("userList",userList);
        return "/user-table" ;
    }
    @RequestMapping("/queryUserByUserRname")
    private String queryUserByUserRname(String userRname,HttpServletRequest request){
        List<User> userList = userService.queryUserByUserRname(userRname);
        request.setAttribute("userList",userList);
        return "/user-table" ;
    }
    @RequestMapping("/addUser")
    private String addUser(User user,HttpServletRequest request){
        int i= userService.addUser(user);
        if (i>0){
            request.setAttribute("msg",i);
        }else {
            request.setAttribute("msg",null);
        }

        return "/user-add" ;
    }
    @RequestMapping("/deleteUser")
    private String deleteUser(String userID,HttpServletRequest request){
        int i= userService.delUser(userID);
        List<User> userList = userService.queryUserList();
        request.setAttribute("userList",userList);
        return "/user-table";
    }

}
